package com.bw.liveplay.common.mvvm.viewmodel

import android.os.Bundle
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bw.liveplay.common.entity.BaseEntity
import com.bw.liveplay.common.filed.UIField
import com.bw.liveplay.common.mvvm.mondel.BaseModel
import com.bw.liveplay.common.mvvm.view.IView
import io.reactivex.Observer
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import java.lang.reflect.ParameterizedType

/**
 * @author KK
 * @description:
 * @date :2021/11/9 18:53
 *
 */
 abstract class BaseViewModel<M : BaseModel> : IViewModel,ViewModel(),Observer<BaseEntity>{
    val m :M
     lateinit var compositeDisposable : CompositeDisposable
     var uiChanfeListenter: UIChanfeListenter
    //初始化model对象
    init {
        //反射获取泛型中BaseModel具体类型
        val parameterizedType = this.javaClass.genericSuperclass as ParameterizedType
        //获取全部泛型类型数组
        val types = parameterizedType.actualTypeArguments
        val clazz : Class<*> = types[0] as Class<*>
        m = clazz.newInstance() as M
        uiChanfeListenter = UIChanfeListenter()

    }

    override fun create() {
        compositeDisposable = CompositeDisposable()
        initData()
    }

    override fun destory() {
        compositeDisposable.dispose()
        compositeDisposable.clear()
    }

    fun getUIChange():UIChanfeListenter{
        return  uiChanfeListenter
    }

    //接口请求返回值
   abstract fun result(t: BaseEntity)


    override fun onSubscribe(d: Disposable) {
        compositeDisposable.add(d)
        showDialog()
    }

    override fun onNext(t: BaseEntity) {
        result(t)
    }

    override fun onError(e: Throwable) {
        showMsg(e.message.toString())
    }

    override fun onComplete() {
        hideDialog()
    }

    fun showDialog(){
        getUIChange().setshowDialog(""+System.currentTimeMillis())
    }

    fun hideDialog(){
        getUIChange().sethideDialog(""+System.currentTimeMillis())
    }

    fun showMsg(msg : String){
        getUIChange().setshowMsg(msg)
    }

    fun startPage(clazz: Class<*>,bundle: Bundle){
        val map : HashMap<String,Any> = HashMap<String,Any>()
        map.put(UIField.PAGECLAZZ,clazz)
        if(bundle!=null){
            map.put(UIField.PAGEBUNDLE,bundle)
        }
        getUIChange().startPage(map)
    }

    fun finishPage(){
        getUIChange().finishPage(""+System.currentTimeMillis())
    }

    class UIChanfeListenter{
        var showDialogLiveData : MutableLiveData<String> ?=null
           get() {
               if(field == null){
                   field = MutableLiveData<String>()
               }
               return field
           }

         var hideDialogLiveData : MutableLiveData<String> ?=null
            get() {
                if(field == null){
                    field = MutableLiveData<String>()
                }
                return field
            }

         var showmsgLiveData : MutableLiveData<String> ?=null
            get() {
                if(field == null){
                    field = MutableLiveData<String>()
                }
                return field
            }

         var startPageLiveData : MutableLiveData<HashMap<String,*>> ?=null
            get() {
                if(field == null){
                    field = MutableLiveData<HashMap<String,*>>()
                }
                return field
            }

         var finishPageLiveData : MutableLiveData<String> ?=null
            get() {
                if(field == null){
                    field = MutableLiveData<String>()
                }
                return field
            }

        fun setshowDialog(str : String){
            showDialogLiveData!!.value = str
        }
        fun sethideDialog(str : String){
            hideDialogLiveData!!.value = str
        }
        fun setshowMsg(str : String){
            showmsgLiveData!!.value = str
        }
        fun finishPage(str : String){
            finishPageLiveData!!.value = str
        }
        fun startPage(map :HashMap<String,*>){
            startPageLiveData!!.value = map
        }

    }

   abstract fun initData()





}